package com.cj.profile;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Profile;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Map;

/**
 * @author cc
 * @date 2025-05-12 20:59
 **/

public class MyProfileCondition implements Condition {
//    @Profile()
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        // 直接返回原始类型，比如 String
        Map<String, Object> attrs = metadata.getAnnotationAttributes(MyProfile.class.getName());


        //统一包装为 List<Object>，哪怕只有一个值
//        MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(MyProfile.class.getName());
        if (attrs != null){
            String value = (String) attrs.get("value");
            System.out.println("value = " + value);
            System.out.println(context.getEnvironment().matchesProfiles(value));
            if (context.getEnvironment().matchesProfiles(value)) {
                return true;
            }
           return false;
        }
        return false;
    }
}
